package com.iyao_app.util;

import android.text.TextUtils;

import com.amap.api.maps.model.LatLng;

import java.math.BigDecimal;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/28.
 */

public class DistanceUtil {
    public static String getDitance(String distance) {
        if (TextUtils.isEmpty(distance)) {
            return "0m";
        }
        try {
            double v = Double.parseDouble(distance);

            if (v > 1) {
                return v + "km";
            } else {
                int v1 = (int) (v * 1000);
                return v1 + "m";
            }
        } catch (Exception e) {
            BigDecimal bd = new BigDecimal(distance);
            double v = Double.parseDouble(bd.toPlainString());
            if (v > 1) {
                return v + "km";
            } else {
                int v1 = (int) (v * 1000);
                return v1 + "m";
            }
        }
    }

    public static String getDitance(double v) {
        try {
            if (v > 1) {
//                BigDecimal df = new BigDecimal(i);
//                mCount = df.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "w";
                return v + "km";
            } else {
                int v1 = (int) (v * 1000);
                return v1 + "m";
            }
        } catch (Exception e) {
            return "0m";
        }
    }


    public static String getDistance(LatLng otherLng, LatLng myLatLng) {
        double radLat1 = rad(myLatLng.latitude);
        double radLat2 = rad(otherLng.latitude);
        double a = radLat1 - radLat2;
        double b = rad(myLatLng.longitude) - rad(otherLng.longitude);
        double s = 2 * Math.asin(
                Math.sqrt(
                        Math.pow(Math.sin(a / 2), 2)
                                + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)
                )
        );
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000000;
        return getDitance(s);
    }

    public static double getDistance_(LatLng otherLng, LatLng myLatLng) {
        double radLat1 = rad(myLatLng.latitude);
        double radLat2 = rad(otherLng.latitude);
        double a = radLat1 - radLat2;
        double b = rad(myLatLng.longitude) - rad(otherLng.longitude);
        double s = 2 * Math.asin(
                Math.sqrt(
                        Math.pow(Math.sin(a / 2), 2)
                                + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)
                )
        );
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;
    }

    //把经纬度转为度（°）
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    //地球平均半径
    private static final double EARTH_RADIUS = 6378137;
}
